//
//  HomeBloodOxygenCell.swift
//  DaRings
//
//  Created by 尹琼 on 2022/10/27.
//

import UIKit
import MYModule

import RxCocoa
import RxSwift

class HomeBloodOxygenCell: HomeBaseCell {
    
    var viewModel: HomeBloodOxygenViewModel?

    override func makeUI() {
        super.makeUI()
        
       
        self.iconImagView.image = UIImage(named: "ic_home_today_blood_oxygen")
        self.titleLabel.text = "blood_oxygen_title".language

        bgView.addSubview(bloodOxygenStackView)
        bgView.addSubview(rangeLbl)
        bgView.addSubview(progressView)
        bgView.addSubview(stackView)
        bgView.addSubview(measureButton)
        
        measureButton.rx.tap.subscribe(onNext: {[weak self] in
            
            BluetoothResponder.shared.willMeasurement {
                if let isSelected = self?.measureButton.isSelected {
                    self?.measureButton.isSelected = !isSelected
                }
                self?.viewModel?.automaticMeasure()
            }
//            self?.reloadData()
        }).disposed(by: measureButton.rx.disposeBag)
        

        bloodOxygenStackView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-15)
            make.centerY.equalTo(titleLabel)
        }
        
        rangeLbl.snp.makeConstraints { make in
            make.top.equalTo(bloodOxygenStackView.snp.bottom).offset(4)
            make.trailing.equalTo(unitLbl.snp.trailing)
        }
        
        progressView.snp.makeConstraints { make in
            make.top.equalTo(dateLabel.snp.bottom).offset(20)
            make.leading.equalToSuperview().offset(16)
            make.trailing.equalToSuperview().offset(-16)
            make.height.equalTo(24)
        }

        stackView.snp.makeConstraints { make in
            make.top.equalTo(progressView.snp.bottom).offset(6)
            make.leading.equalToSuperview().offset(14)
            make.centerX.equalToSuperview()
        }
        
        measureButton.snp.makeConstraints { make in
            make.top.equalTo(stackView.snp.bottom).offset(20)
            make.leading.equalToSuperview().offset(15)
            make.height.equalTo(52)
            make.bottom.trailing.equalToSuperview().offset(-15)
        }
    }
    
    
    
    func bindButton(measureState: UIControl.State) {
        
        if measureState == .selected {
            
            measureButton.isEnabled = true
            measureButton.isSelected = true
            
        } else if measureState == .normal {
            
            measureButton.isEnabled = true
            measureButton.isSelected = false
            
        } else if measureState == .disabled {
            
            measureButton.isEnabled = false
            measureButton.isSelected = false
        }
        
    }
    
    override func bind(to viewModel: ViewModel?) {
        super.bind(to: viewModel)
        
        guard let viewModel = viewModel as? HomeBloodOxygenViewModel else {
            return
        }
        
        self.viewModel = viewModel
        
        let disposeBag = DisposeBag()
        viewModel.UIReplay.subscribe(onNext: {[weak self] in
            
            self?.bindButton(measureState: $0.measureState)
            
            self?.dateLabel.text = $0.date
            self?.currentNumLbl.text = $0.bloodOxygenTitle
            self?.progressView.o2 = $0.bloodOxygen
            
            
        }).disposed(by: disposeBag)

        viewModel.hasDevicedReplay.subscribe(onNext: {[weak self] in
            
            self?.updateLayouts(hasDevice: $0)
            
        }).disposed(by: rx.disposeBag)
    
    }
    
    func updateLayouts(hasDevice: Bool?) {
        
        let hidden = hasDevice == true ? false : true
        
        measureButton.isHidden = hidden

        if !hidden {
            
            measureButton.snp.remakeConstraints { make in
                make.top.equalTo(stackView.snp.bottom).offset(20)
                make.leading.equalToSuperview().offset(15)
                make.trailing.equalToSuperview().offset(-15)
                make.height.equalTo(52)
                make.bottom.equalToSuperview().offset(-15)
                
            }
            
        } else {
            
            measureButton.snp.remakeConstraints { make in
                make.top.equalTo(stackView.snp.bottom)
                make.leading.equalToSuperview().offset(15)
                make.trailing.equalToSuperview().offset(-15)
                make.height.equalTo(0)
                make.bottom.equalToSuperview().offset(-15)
                
            }
        }
        
    }

    private lazy var bloodOxygenStackView: StackView = {
        let bloodOxygenStackView = StackView()
        bloodOxygenStackView.alignment = .firstBaseline
        bloodOxygenStackView.axis = .horizontal
        bloodOxygenStackView.distribution = .fill
        bloodOxygenStackView.spacing = 4
        bloodOxygenStackView.addArrangedSubview(currentNumLbl)
        bloodOxygenStackView.addArrangedSubview(unitLbl)
        return bloodOxygenStackView
    }()
    
    public lazy var currentNumLbl: Label = {
        let currentNumLbl = Label()
        currentNumLbl.textAlignment = .right
        currentNumLbl.textColor = UIColor.BloodOxygen.theme
        currentNumLbl.font = Fonts.boldSystemFont(ofSize: 28)
        currentNumLbl.text = "97"
        return currentNumLbl
    }()

    public lazy var unitLbl: Label = {
        let untilLbl = Label()
        untilLbl.textAlignment = .right
        untilLbl.textColor = UIColor.assist_222222
        untilLbl.font = Fonts.systemFont(ofSize: 14)
        untilLbl.text = "%"
        return untilLbl
    }()

    public lazy var rangeLbl: Label = {
        let rangeLbl = Label()
        rangeLbl.font = Fonts.systemFont(ofSize: 10)
        rangeLbl.textColor = UIColor.assist_D0D0D0
        rangeLbl.textAlignment = .right
        rangeLbl.text = "95~100%"
        return rangeLbl
    }()
    
    private lazy var progressView: TodayBloodOxygenProgressView = {
        let progressView = TodayBloodOxygenProgressView()
        progressView.configColor(bgColor: UIColor.assist_EEEEEE, progressColor: UIColor.BloodOxygen.theme)
        return progressView
    }()
    
    
    public lazy var stackView: StackView = {
        let stackView = StackView()
        stackView.alignment = .center
        stackView.axis = .horizontal
        stackView.distribution = .equalSpacing
        for title in ["80%", "85%", "90%", "95%", "100%"] {
            let numberLabel = Label()
            numberLabel.textColor = UIColor.assist_D0D0D0
            numberLabel.font = Fonts.systemFont(ofSize: 10)
            numberLabel.text = title
            stackView.addArrangedSubview(numberLabel)
        }
        return stackView
    }()
    
    private lazy var measureButton: HomeMeasureButton = {
        let measureButton = HomeMeasureButton()
        measureButton.setTitle("blood_oxygen_measure_star_btn_title".language, for: .normal)
        measureButton.setTitle("blood_oxygen_measure_end_btn_title".language, for: .selected)
        
        let image = UIImage(named: "ic_home_today_blood_oxygen_measure")
        measureButton.setImage(image, for: .normal)
        
        let disableTitleColor = UIColor.assist_A9A9A9
        let disableImage = image?.originalImageWithColor(color: disableTitleColor)
        
        measureButton.setImage(disableImage, for: .disabled)
        measureButton.setTitleColor(disableTitleColor, for: .disabled)
        
        measureButton.setTitleColor(UIColor.assist_222222, for: .normal)
        measureButton.titleLabel?.font = Fonts.systemFont(ofSize: 14)
        measureButton.backgroundColor = UIColor.assist_F8F8F8
        measureButton.setImageTitleLayout(.imgLeft, spacing: 7)
        measureButton.cornerRadius = 14
        return measureButton
    }()
}
